#####################
########INDEX########
#####################

#MARIANA CARMO DUARTE

#AUGUST 2024

#Libraries
library(haven)
library(foreign)
library(scales)
library(dplyr)
library(tidyverse)

# load data
setwd("")
politicisation <- read.csv2("PoliticalParties_CHES.csv")

#Parties in parliament
politicisation <- subset(politicisation, seat_share_n > 0)

#Create the grouping variable countryear
politicisation$countryear <- paste0(politicisation$country, politicisation$year)

######################
#POLITICISATION INDEX#
######################

#Remove NA's
politicisation <- politicisation%>%drop_na(eu_salience_r1, eu_position_r1)

#Dalton Index
politicisation$averpos <- ave(politicisation$eu_position_r1, politicisation$election_id, FUN = mean)
politicisation$v.distance <- politicisation$seat_share_n * ((politicisation$eu_position_r1 - politicisation$averpos)/5)^2
politicisation$sum.dist <- ave(politicisation$v.distance, politicisation$countryear, FUN = sum)
politicisation$Dalton <- sqrt(politicisation$sum.dist)
summary(politicisation$Dalton)

#Dalton Index (salience)
politicisation$s.v.distance <- politicisation$eu_salience_r1 * politicisation$v.distance
politicisation$sum.s.dist <- ave(politicisation$s.v.distance, politicisation$election_id, FUN = sum)
politicisation$salience.Dalton <- sqrt(politicisation$sum.s.dist)
summary(politicisation$salience.Dalton)

#POLITICISATION INDEX

#POLARISATION
politicisation$averpos_p <- ave(politicisation$eu_position_r1, politicisation$election_id, FUN = mean)
politicisation$dist_pol <- ((politicisation$eu_position_r1 - politicisation$averpos_p)/5)^2
politicisation$w.dist_pol <- politicisation$seat_share_n*politicisation$dist_pol
politicisation$sum_p <- ave(politicisation$w.dist_pol, politicisation$countryear, FUN = sum)
politicisation$eu_polarisation_ps <- sqrt(politicisation$sum_p)
summary(politicisation$eu_polarisation_ps)

#SALIENCE
politicisation$salience.w <- politicisation$eu_salience_r1*politicisation$seat_share_n
politicisation$eu_salience_ps <- ave(politicisation$salience.w, politicisation$election_id, FUN = sum)/100 
politicisation$eu_salience_d <- politicisation$eu_salience_ps/2
politicisation$eu_salience_ps[politicisation$countryear=="be  1999"] <- politicisation$eu_salience_d[politicisation$countryear=="be  1999"]
politicisation$eu_salience_ps[politicisation$countryear=="be  2002"] <- politicisation$eu_salience_d[politicisation$countryear=="be  2002"]
politicisation$eu_salience_ps[politicisation$countryear=="cz  2017"] <- politicisation$eu_salience_d[politicisation$countryear=="cz  2017"]
politicisation$eu_salience_ps[politicisation$countryear=="cz  2019"] <- politicisation$eu_salience_d[politicisation$countryear=="cz  2019"]
politicisation$eu_salience_ps[politicisation$countryear=="fin 1999"] <- politicisation$eu_salience_d[politicisation$countryear=="fin 1999"]
politicisation$eu_salience_ps[politicisation$countryear=="fin 2002"] <- politicisation$eu_salience_d[politicisation$countryear=="fin 2002"]
politicisation$eu_salience_ps[politicisation$countryear=="fr  2002"] <- politicisation$eu_salience_d[politicisation$countryear=="fr  2002"]
politicisation$eu_salience_ps[politicisation$countryear=="fr  2006"] <- politicisation$eu_salience_d[politicisation$countryear=="fr  2006"]
politicisation$eu_salience_ps[politicisation$countryear=="fr  2017"] <- politicisation$eu_salience_d[politicisation$countryear=="fr  2017"]
politicisation$eu_salience_ps[politicisation$countryear=="fr  2019"] <- politicisation$eu_salience_d[politicisation$countryear=="fr  2019"]
politicisation$eu_salience_ps[politicisation$countryear=="ge  2017"] <- politicisation$eu_salience_d[politicisation$countryear=="ge  2017"]
politicisation$eu_salience_ps[politicisation$countryear=="ge  2019"] <- politicisation$eu_salience_d[politicisation$countryear=="ge  2019"]
politicisation$eu_salience_ps[politicisation$countryear=="hun 2014"] <- politicisation$eu_salience_d[politicisation$countryear=="hun 2014"]
politicisation$eu_salience_ps[politicisation$countryear=="hun 2017"] <- politicisation$eu_salience_d[politicisation$countryear=="hun 2017"]
politicisation$eu_salience_ps[politicisation$countryear=="irl 2002"] <- politicisation$eu_salience_d[politicisation$countryear=="irl 2002"]
politicisation$eu_salience_ps[politicisation$countryear=="irl 2006"] <- politicisation$eu_salience_d[politicisation$countryear=="irl 2006"]
politicisation$eu_salience_ps[politicisation$countryear=="it  2014"] <- politicisation$eu_salience_d[politicisation$countryear=="it  2014"]
politicisation$eu_salience_ps[politicisation$countryear=="it  2017"] <- politicisation$eu_salience_d[politicisation$countryear=="it  2017"]
politicisation$eu_salience_ps[politicisation$countryear=="nl  2017"] <- politicisation$eu_salience_d[politicisation$countryear=="nl  2017"]
politicisation$eu_salience_ps[politicisation$countryear=="nl  2019"] <- politicisation$eu_salience_d[politicisation$countryear=="nl  2019"]
politicisation$eu_salience_ps[politicisation$countryear=="slo 2017"] <- politicisation$eu_salience_d[politicisation$countryear=="slo 2017"]
politicisation$eu_salience_ps[politicisation$countryear=="slo 2019"] <- politicisation$eu_salience_d[politicisation$countryear=="slo 2019"]
politicisation$eu_salience_ps[politicisation$countryear=="swe 2014"] <- politicisation$eu_salience_d[politicisation$countryear=="swe 2014"]
politicisation$eu_salience_ps[politicisation$countryear=="swe 2017"] <- politicisation$eu_salience_d[politicisation$countryear=="swe 2017"]
politicisation$eu_salience_ps[politicisation$countryear=="uk  2010"] <- politicisation$eu_salience_d[politicisation$countryear=="uk  2010"]
politicisation$eu_salience_ps[politicisation$countryear=="uk  2014"] <- politicisation$eu_salience_d[politicisation$countryear=="uk  2014"]
politicisation$eu_salience_ps[politicisation$countryear=="esp 1996"] <- politicisation$eu_salience_d[politicisation$countryear=="esp 1996"]
politicisation$eu_salience_ps[politicisation$countryear=="esp 1999"] <- politicisation$eu_salience_d[politicisation$countryear=="esp 1999"]
politicisation$eu_salience_ps[politicisation$countryear=="fr  1988"] <- politicisation$eu_salience_d[politicisation$countryear=="fr  1988"]
politicisation$eu_salience_ps[politicisation$countryear=="fr  1992"] <- politicisation$eu_salience_d[politicisation$countryear=="fr  1992"]
politicisation$eu_salience_ps[politicisation$countryear=="gr  1996"] <- politicisation$eu_salience_d[politicisation$countryear=="gr  1996"]
politicisation$eu_salience_ps[politicisation$countryear=="gr  1999"] <- politicisation$eu_salience_d[politicisation$countryear=="gr  1999"]
politicisation$eu_salience_ps[politicisation$countryear=="irl 1992"] <- politicisation$eu_salience_d[politicisation$countryear=="irl 1992"]
politicisation$eu_salience_ps[politicisation$countryear=="irl 1999"] <- politicisation$eu_salience_d[politicisation$countryear=="irl 1999"]
politicisation$eu_salience_ps[politicisation$countryear=="uk  1992"] <- politicisation$eu_salience_d[politicisation$countryear=="uk  1992"]
politicisation$eu_salience_ps[politicisation$countryear=="uk  1996"] <- politicisation$eu_salience_d[politicisation$countryear=="uk  1996"]
summary(politicisation$eu_salience_ps)
summary(politicisation$eu_salience)

#POLITICISATION
politicisation$politicisation <- politicisation$eu_polarisation_ps*politicisation$eu_salience_ps
summary(politicisation$politicisation)

setwd("")
write_sav(politicisation, "PoliticalParties_CHES_ELECTIONS.sav")
write.csv2(politicisation, "PoliticalParties_CHES_ELECTIONS.csv")
